<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;

/**
 * 通用上传接口
 * Class Upload
 * @package app\api\controller
 */
class Hsjson extends Controller
{
    protected function _initialize()
    {
        parent::_initialize();

        if (!Session::has('admin_id')) {
            $result = [
                'error'   => 1,
                'message' => '未登录'
            ];
            echo json_encode($result);exit;
        }
    }

    /**
     * 通用图片上传接口
     * @return \think\response\Json
     */
    public function upload()
    {
        $config = [
            'size' => 2097152000,
            'ext'  => 'jpg,gif,png,jpeg,bmp'
        ];
        $file = $this->request->file('file');

        if($file){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'uploads/uedit/image/');

            $save_path   = '/uploads/uedit/image/';
            $info        = $file->validate($config)->move($upload_path);

            if ($info) {
                $result = [
                    'error' => 0,
                    'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
                ];
            } else {
                $result = [
                    'error'   => 1,
                    'message' => $file->getError()
                ];
            }
            exit(json_encode($result));//text/html，用于解决ie浏览器上传后弹出下载框
            //return json($result);//json数据
        }
    }
    public function admin_img(){
        $config = [
            'size' => 100000,
        ];
        $file = $this->request->file('file');

        if($file){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'uploads/uedit/image/');

            $save_path   = '/uploads/uedit/image/';
            $info        = $file->validate($config)->move($upload_path);

            if ($info) {    //上传成功后写入数据库
                $url = str_replace('\\', '/', $save_path . $info->getSaveName());
                if(db('admin_user')->where(['id'=>session('admin_id')])->update(['img'=>$url]) === false){
                    $result = ['error' => 1,'message' =>'请重试'];
                }else{
                    session('admin_img',$url);
                    $result = ['error' => 0,'url' =>$url];
                }
            } else {
                $result = [
                    'error'   => 1,
                    'message' => $file->getError()
                ];
            }
            exit(json_encode($result));//text/html，用于解决ie浏览器上传后弹出下载框
            //return json($result);//json数据
        }
    }
    /**
     * 通用图片上传接口
     * @return \think\response\Json
     */
    public function upload_video()
    {
        $config = [
            'size' => 20971520000,
            'ext'  => 'mp4'
        ];
        $file = $this->request->file('file');

        if($file){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'uploads/uedit/file/');

            $save_path   = '/uploads/uedit/file/';
            $info        = $file->validate($config)->move($upload_path);

            if ($info) {
                $result = [
                    'error' => 0,
                    'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
                ];
            } else {
                $result = [
                    'error'   => 1,
                    'message' => $file->getError()
                ];
            }
            exit(json_encode($result));//text/html，用于解决ie浏览器上传后弹出下载框
            //return json($result);//json数据
        }
    }
}